<?php

/**
 * Common Warlock Plugin
 *
 * Warlock is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 *
 * @package    warlock
 * @author     David Cole <neophyte@sourcetutor.com>
 * @license    GNU/GPL v2
 */

// Dependencies
using('Extensions.IPlugin');

class Plugin_Common_warlock extends IPlugin
{
    /**#@+
     * Required Core Objects
     *
     * These are references to the core objects required by this
     * class.
     *
     * @access private
     * @var    object
     */
    var $_Error;
    var $_DAL;
    var $_Session;
    /**#@-*/

    function __construct()
    {
        // IPlugin Constructor
        parent::__construct();

        // Reference Required Objects
        $this->_Error   = &Vault::Get('Debug_Error');
        $this->_DAL     = &Vault::Get('DAL');
        $this->_Session = &Vault::Get('Session');
    }

    function onAuthenticate($username, $password)
    {
        if (!($user = $this->_DAL->Select('user', array('username' => $username), null, 1)))
            return false;
        else
        {
            if ($this->_ValidatePassword($password, $user->password))
            {
                $this->_Session->Set('user', $user->id);
                return $user;
            }
            else
                return false;
        }
    }

    function onLogout($id, $username)
    {
        $this->_Session->Clear();

        return true;
    }

    function _ValidatePassword($compare, $against)
    {
		$stored_seed = substr($against, 32, 8);

		if (md5($stored_seed.$compare).$stored_seed == $against)
			return true;
		else
			return false;
    }
}

?>